//
//  ViewController.swift
//  19.1 scrollview
//
//  Created by 李煜 on 2024/6/26.
//https://www.jianshu.com/p/f0eeef56f391
//contentsize是内容尺寸,不包括insets
//https://www.jianshu.com/p/9091e5f34df5
import UIKit

class ViewController: UIViewController,UIScrollViewDelegate {
    var headerImageView: UIImageView!
    
    lazy var scrollView: UIScrollView = {
        let s = UIScrollView(frame: view.bounds)
        s.delegate = self
        //不可以设置bounces=true
//        s.bounces = false
        //icon
        let imageView = UIImageView(image: UIImage(named: "cats"))
        imageView.contentMode = .scaleToFill
        imageView.frame = CGRectMake(0, -200, view.bounds.width, 200)
        headerImageView = imageView
        
        let blue = UILabel(frame: CGRectMake(0, 0, view.bounds.width, 500))
        blue.backgroundColor = .blue
        blue.text = "高度500"
        blue.textColor = .white
        let cyan = UILabel(frame: CGRectMake(0, blue.frame.maxY, view.bounds.width, 500))
        cyan.backgroundColor = .cyan
        cyan.text = "高度500"


        s.addSubview(imageView)
        s.addSubview(blue)
        s.addSubview(cyan)
        
        view.addSubview(s)
        
        s.contentInset = UIEdgeInsets(top: 200, left: 0, bottom: 0, right: 0)
        s.contentSize = CGSizeMake(0, 500 + 500)
        return s
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        scrollView.backgroundColor = .brown
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print(scrollView.contentOffset)
        let point = scrollView.contentOffset
        if point.y < -200 {
            self.headerImageView.frame.origin.y = point.y
            self.headerImageView.frame.size.height = -point.y
        }
    }
}

